package com.framework.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.framework.entity.AuthInfoFormMap;
import com.framework.entity.CorpAuthFormMap;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
* @author Lyon:
* @version 创建时间：2016年11月9日 下午6:49:46
* 类说明
*/
public class WXUtil {

	public static boolean saveAuthCropInfo(JSONObject parajson, String suite_id, HashMap<String, Object> hashMap) throws Exception {
		List<Object> list=new ArrayList<>();
		JSONObject json = parajson;
		if (json.isEmpty()) {
			String nothing;
		} else {
			if (json.containsKey("permanent_code")) {
				
				Date addate = new Date();
				Date sysdate;
				DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				Calendar cal = Calendar.getInstance();
				cal.setTime(addate);
				cal.add(Calendar.SECOND, json.getInt("expires_in") - 300);
				sysdate = cal.getTime();
				
				// 保存TOKEN
				CorpAuthFormMap corpAuthFormMap = new CorpAuthFormMap();
				corpAuthFormMap.put("Auth_corpid", json.getJSONObject("auth_corp_info").get("corpid"));
				corpAuthFormMap.put("AccessToken", json.get("access_token"));
				corpAuthFormMap.put("AccessExpires", sysdate);
				corpAuthFormMap.put("suite_id", suite_id);
				corpAuthFormMap.put("corpid", json.getJSONObject("auth_corp_info").get("corpid"));
				corpAuthFormMap.put("permanent_code", json.get("permanent_code"));
				corpAuthFormMap.put("corp_name", json.getJSONObject("auth_corp_info").get("corp_name"));
				corpAuthFormMap.put("corp_type", json.getJSONObject("auth_corp_info").get("corp_type"));
				corpAuthFormMap.put("corp_round_logo_url",
						json.getJSONObject("auth_corp_info").get("corp_round_logo_url"));
				corpAuthFormMap.put("corp_square_logo_url",
						json.getJSONObject("auth_corp_info").get("corp_square_logo_url"));
				corpAuthFormMap.put("corp_user_max", json.getJSONObject("auth_corp_info").get("corp_user_max"));
				corpAuthFormMap.put("corp_agent_max", json.getJSONObject("auth_corp_info").get("corp_type"));
				corpAuthFormMap.put("corp_full_name", json.getJSONObject("auth_corp_info").get("corp_full_name"));
				corpAuthFormMap.put("verified_end_time", json.getJSONObject("auth_corp_info").get("corp_type"));
				corpAuthFormMap.put("subject_type", json.getJSONObject("auth_corp_info").get("subject_type"));
				corpAuthFormMap.put("corp_wxqrcode", json.getJSONObject("auth_corp_info").get("corp_wxqrcode"));
				// 保存授权信息
				corpAuthFormMap=corpAuthFormMap.replace();
				hashMap.put("ca_id", corpAuthFormMap.get("id"));
//				AuthUserInfoFormMap authUserInfoFormMap = new AuthUserInfoFormMap();
//				authUserInfoFormMap.put("corpid", json.getJSONObject("auth_corp_info").get("corpid"));
//				authUserInfoFormMap.put("email", json.getJSONObject("auth_user_info").get("email"));
//				authUserInfoFormMap.put("mobile", json.getJSONObject("auth_user_info").get("mobile"));
//				authUserInfoFormMap.put("userid", json.getJSONObject("auth_user_info").get("userid"));
//				// 保存授权人信息
//				authUserInfoFormMap.save();
				JSONArray agents = json.getJSONObject("auth_info").getJSONArray("agent");
				if (!agents.isEmpty()) {
					for (int i = 0; i < agents.size(); i++) {
						JSONObject agentjson = new JSONObject();
						AuthInfoFormMap authInfoFormMap = new AuthInfoFormMap();
						authInfoFormMap.put("suite_id", suite_id);
						authInfoFormMap.put("corpid", json.getJSONObject("auth_corp_info").get("corpid"));
						authInfoFormMap.put("is_new_auth", json.getJSONObject("auth_info").get("is_new_auth"));
						authInfoFormMap.put("agentid", agents.getJSONObject(i).get("agentid"));
						authInfoFormMap.put("name", agents.getJSONObject(i).get("name"));
						authInfoFormMap.put("round_logo_url", agents.getJSONObject(i).get("round_logo_url"));
						authInfoFormMap.put("square_logo_url", agents.getJSONObject(i).get("square_logo_url"));
						authInfoFormMap.put("appid", agents.getJSONObject(i).get("appid"));
						if (!agents.getJSONObject(i).getJSONObject("privilege").isEmpty()) {
							authInfoFormMap.put("level",
									agents.getJSONObject(i).getJSONObject("privilege").get("level"));
							authInfoFormMap.put("allow_party",
									agents.getJSONObject(i).getJSONObject("privilege").get("allow_party").toString());
							authInfoFormMap.put("allow_user",
									agents.getJSONObject(i).getJSONObject("privilege").get("allow_user").toString());
							authInfoFormMap.put("allow_tag",
									agents.getJSONObject(i).getJSONObject("privilege").get("allow_tag").toString());
							authInfoFormMap.put("extra_party",
									agents.getJSONObject(i).getJSONObject("privilege").get("extra_party").toString());
							authInfoFormMap.put("extra_user",
									agents.getJSONObject(i).getJSONObject("privilege").get("extra_user").toString());
							authInfoFormMap.put("extra_tag",
									agents.getJSONObject(i).getJSONObject("privilege").get("extra_tag").toString());
						}
						// 保存授权的应用信息
						authInfoFormMap=authInfoFormMap.replace();
						list.add(authInfoFormMap.get("id"));
					}
					hashMap.put("ai_id_list", list);
					return true;
				}
			}
		}
		return false;
	}
}
